/**
 * 属性参数
 * @member props
 * @property {String} [primaryField] 主键（每一条数据的唯一标识）
 *
 **/

export default {
  props: {
    primaryField: {
      type: String,
      default: 'id'
    }
  },
  data() {
    return {
      shopList: [],
      currPageSelection: []
    }
  },
  methods: {
    /**
     * 选中事件
     * @param selection
     */
    doSelect(selection) {
      const key = this.primaryField
      const removedArr = this.getRemoved(selection)
      this.currPageSelection = [].concat(selection)

      const list = this.shopList
      const nList = []
      const len = list.length

      // 新选择的
      selection.forEach((o) => {
        const value = o[key]
        if (len === 0) {
          nList.push(o)
        } else {
          // 去除重复的
          let exist = false
          for (let i = 0; i < len; i++) {
            const item = list[i][key]
            if (item === value) {
              exist = true
              break
            }
          }
          if (!exist) {
            nList.push(o)
          }
        }
      })
      if (nList.length > 0) {
        this.shopList = list.concat(nList)
        this.shopList = [].concat(this.shopList)
      }

      // 去掉勾选删除的
      removedArr.forEach((obj) => {
        const i = this.shopList.findIndex((item) => {
          return item[key] === obj[key]
        })

        if (i >= 0) {
          this.shopList.splice(i, 1)
        }
      })
    },
    /**
     * 全选事件
     * @param selection
     */
    doSelectAll(selection) {
      this.onSelect(selection)
    },
    /**
     * 需要移除的数据
     * @param selection
     * @returns {*|Array}
     */
    getRemoved(selection) {
      return this.calculateSelection(this.currPageSelection, selection, this.primaryField)
    },
    calculateSelection(arr1, arr2, prop = 'jh') {
      const rs = []
      arr1.forEach((o1) => {
        if (!this.exist(o1, arr2, prop)) {
          rs.push(o1)
        }
      })
      return rs
    },
    /**
     * 是否已经被选择
     * @param ele
     * @param arr
     * @param prop
     * @returns {boolean}
     */
    exist(ele, arr, prop) {
      const existEl = arr.find((item) => {
        return item[prop] === ele[prop]
      })
      return !!existEl
    },
    /**
     * 选中回来事件
     * @param list
     */
    onSelectedOk(list) {
      this.shopList = [].concat(list)
      this.updateSelection()
    },
    /**
     * 移除
     * @param item
     * @param index
     */
    onRemove(item, index) {
      this.shopList.splice(index, 1)
      this.updateSelection()
    },
    /**
     * 根据最新的shopList修改选中
     */
    updateSelection() {
      const temp = []
      // 修改选中状态
      this.currPageSelection.forEach((o) => {
        if (this.exist(o, this.shopList, this.primaryField)) {
          temp.push(o)
        }
      })

      if(this.$table) {
        this.$table.clearSelection()
        temp.forEach((o) => {
          this.$table.toggleRowSelection(o)
        })
      }

      this.currPageSelection = temp
    },
    getSelectedData() {
      return this.shopList
    },
    setDataChecked(list, selectedList) {
      const currPageSelection = []
      const selectionIndex = []
      const primaryField = this.primaryField

      let selecteds = []
      if(selectedList) {
        selecteds = selectedList
      } else {
        selecteds = this.shopList
      }

      list.forEach((obj, index) => {
        const i = selecteds.findIndex((item) => {
          return item[primaryField] === obj[primaryField]
        })
        if(i >= 0) {
          selectionIndex.push(index)
          currPageSelection.push(obj)
        }
      })

      this.currPageSelection = currPageSelection

      this.$nextTick(() => {
        setTimeout(() => {
          if(selectionIndex.length === 0) {
            this.$table && this.$table.clearSelection()
          } else {
            selectionIndex.forEach((o) => {
              this.$table && this.$table.toggleRowSelection(list[o])
            })
          }
        }, 50)
      })
    }
  },
  watch: {
    shopList(nval, oval) {
      if(nval !== oval) {
        this.$emit('select-changed', nval)
      }
    }
  }
}
